import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QHBoxLayout
from PyQt5.QtGui import QPixmap, QFont
from PyQt5.QtCore import QTimer, QDateTime, Qt


class TopView(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        self.setWindowTitle("Top View Example")
        #self.setGeometry(300, 200, 800, 600)
        self.setFixedSize(1500, 1000) 
        # 创建顶部区域label_bg
        label_bg = QLabel(self)
        label_bg.setPixmap(QPixmap('views/assets/header_bg.png').scaled(self.width(), 200, Qt.KeepAspectRatioByExpanding))
        label_bg.setAlignment(Qt.AlignTop | Qt.AlignHCenter)

        # 创建一个嵌套区域（水平布局）
        inner_layout = QHBoxLayout()

        # label_left：显示当前时间
        label_left = QLabel(self)
        label_left.setFont(QFont("Arial", 14))
        label_left.setStyleSheet("color: white;")
        label_left.setAlignment(Qt.AlignLeft)
        self.update_time(label_left)  # 初始化时间显示
        timer = QTimer(self)
        timer.timeout.connect(lambda: self.update_time(label_left))  # 定时更新时间
        timer.start(1000)  # 每秒更新时间

        # label_center：显示图片
        label_center = QLabel(self)
        label_center.setPixmap(QPixmap('views/assets/header_title.png').scaled(100, 100, Qt.KeepAspectRatio))
        label_center.setAlignment(Qt.AlignCenter)

        # 将label_left和label_center添加到嵌套的水平布局
        inner_layout.addWidget(label_left)
        inner_layout.addWidget(label_center)

        # 创建一个外部垂直布局，用来放置背景区域和嵌套区域
        main_layout = QVBoxLayout(self)
        main_layout.addWidget(label_bg)
        main_layout.addLayout(inner_layout)  # 将嵌套的布局加入到主布局

        self.setLayout(main_layout)

    def update_time(self, label):
        """更新时间"""
        current_time = QDateTime.currentDateTime()
        time_str = current_time.toString("yyyy-MM-dd HH:mm:ss")
        label.setText(time_str)  # 设置时间文本


if __name__ == "__main__":
    app = QApplication(sys.argv)
    topview = TopView()
    topview.show()
    sys.exit(app.exec_())
